<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入应用名称标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="添加时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择添加时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应用名称" align="center" prop="name"/>
      <el-table-column label="描述" align="center" prop="info"/>
      <el-table-column label="添加时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template #default="scope">
              <el-button link type="primary" icon="View" @click="handlePreview(scope.row)">预览</el-button>
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应用名称对话框 -->
    <el-dialog :title="title" v-model="open">
      <el-form ref="articleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="分类" prop="vcKey">
          <el-input v-model="form.vcKey" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="描述" prop="info">
          <el-input v-model="form.info" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改应用名称对话框 -->
    <el-dialog title="预览" v-model="openPreview" width="80%"
               append-to-body>
      <v-md-preview :text="textPreview"></v-md-preview>
    </el-dialog>
  </div>
</template>

<script setup name="Article">

import { listCategory, getCategory, saveOrUpdateCategory, delCategory } from '@/api/version/category'

const { proxy } = getCurrentInstance();

const list = ref([]);
const labelList = ref([]);
const open = ref(false);
const openPreview = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const textPreview = ref("");
const categoryOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    categoryId: undefined,
    title: undefined,
    context: undefined,
    topping: undefined,
    browse: undefined,
    isShow: undefined,
    createBy: undefined,
    updateBy: undefined,
    updateTime: undefined,
    createTime: undefined
  },
  rules: {
    name: [
      { required: true, message: "应用名称不能为空", trigger: "blur" }
    ],
    vcKey: [
      { required: true, message: "分类不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询应用名称列表 */
function getList() {
  loading.value = true;
  listCategory(queryParams.value).then(response => {
    list.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

function getCatgoryList() {
  loading.value = true;
  listCategory(queryParams.value).then(response => {
    list.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    info: null,
    vcKey: null,
    name: null,
    createTime: null
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加应用名称";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _articleId = row.id || ids.value
  getCategory(_articleId).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改应用名称";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["articleRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      saveOrUpdateCategory(form.value).then(response => {
        proxy.$modal.msgSuccess("操作成功");
        open.value = false;
        getList();
      }).finally(() => {
        buttonLoading.value = false;
      });
    }
  });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除应用名称编号为"' + ids + '"的数据项？').then(function() {
    loading.value = true;
    return delCategory(ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('blog/article/export', {
    ...queryParams.value
  }, `article_${new Date().getTime()}.xlsx`)
}

function mdSave(text, html){
  console.log(form.value.context)
}

function handlePreview(row){
  textPreview.value = row.context
  openPreview.value = true
}

getList();

onActivated(() => {
})
</script>
